package org.lgiavedoni.android.franq.core.log.dao;

import java.util.List;

import org.lgiavedoni.android.franq.core.dao.AbstractDAO;
import org.lgiavedoni.android.franq.core.log.Log;
import org.lgiavedoni.android.franq.service.log.Logger.LogLevel;
import org.lgiavedoni.android.franq.util.collection.CollectionUtils;

import com.db4o.query.Predicate;

public class LogDAO extends AbstractDAO<Log> {

	public static final LogDAO inst = new LogDAO();

	@Override
	public void add(Log o) {
		if (dbService.isStarted()) {
			if (o == null) {
				return;
			}
			// FIXME move to a memory collection
			dbService.saveObject(o);
			checkSize();
		}
	}

	@Override
	public Class getObjectClass() {
		return Log.class;
	}

	public List<Log> getLastErrors(int numOfObj) {
		return CollectionUtils.getLast(executeQueryPredicate(new Predicate<Log>() {

			@Override
			public boolean match(Log l) {
				return LogLevel.ERROR.equals(l.getLogLevel());
			}
		}), numOfObj);
	}
}
